﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class StoredProcedureDocument : Document
    {
        #region Internal
        //Constructor
        internal StoredProcedureDocument(SqlServer.StoredProcedure storedProcedure)
        {
            DocumentType = DocumentType.StoredProcedure;
            RootDatabase = storedProcedure.Schema.Database;
            RootServer = RootDatabase.Server;
            URL = "index." + 
                RootDatabase.Name + "." + 
                storedProcedure.Schema.Name + @".storedprocedure." + 
                storedProcedure.Name + @".html";
            _storedProcedure = storedProcedure;
        }
        //Properties
        internal override string DisplayName { get { return _storedProcedure.Name; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddDdlCssColors();

                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.propertyColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.dataTypeColumn { width: 10%; }");
                AddCssLine(@"      	.directionColumn { width: 10%; }");

                AddBodyLine(@"<div id=""mainBody"">");
                Boolean evenRow = true;

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">" 
                    + ScrubHtml(RootDatabase.Name) + @"</a> . ";
                if (Business.FilterPreferences.ShowSchemasPage)
                {
                   heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">"
                       + _storedProcedure.Schema.Name + @"</a> . ";
                }
                else
                {
                   heirarchy += _storedProcedure.Schema.Name + @" . ";
                }
                heirarchy += ScrubHtml(_storedProcedure.Name) + @"</h2>";
                AddBodyLine(heirarchy);

                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_storedProcedure.Name) + @" (Stored Procedure)</h2>");
                #region Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_properties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('properties');"">Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""properties"">");
                AddBodyLine(@"<table>");
                
                string colGroup = @"<colgroup><col class=""propertyColumn"" /><col class=""valueColumn"" /></colgroup>";
                AddBodyLine(colGroup);

                AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Name</td><td>" + ScrubHtml(_storedProcedure.Name) + @"</td></tr>");
                AddBodyLine(@"<tr class=""oddRow""><td>Schema</td><td>" + ScrubHtml(_storedProcedure.Schema.Name) + @"</td></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Creation Date</td><td>" + _storedProcedure.CreateDate.ToString() + @"</td></tr>");
                if (_storedProcedure.ModifiedDate == null)
                    AddBodyLine(@"<tr class=""oddRow""><td>Last Modified</td><td>N/A</td></tr>");
                else
                    AddBodyLine(@"<tr class=""oddRow""><td>Last Modified</td><td>" + _storedProcedure.ModifiedDate.ToString() + @"</td></tr>");
                string s = @"<tr class=""evenRow""><td>Description</td><td>";
                s += _storedProcedure.Description == "" ? @"nbsp;" : ScrubHtml(_storedProcedure.Description);
                s += @"</td></tr>";
                AddBodyLine(s);

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Parameters
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_parameters"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('parameters');"">Parameters</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""parameters"">");
                AddBodyLine(@"<table>");

                colGroup = @"<colgroup><col class=""nameColumn"" /><col class=""dataTypeColumn"" />" +
                    @"<col class=""directionColumn"" /><col class=""descriptionColumn"" /></colgroup>";
                AddBodyLine(colGroup);
                
                AddBodyLine(@"<tr><th>Name</th><th>Data Type</th><th>Direction</th><th>Description</th></tr>");

                evenRow = true;
                foreach (SqlServer.Parameter parameter in _storedProcedure.Parameters)
                {
                    StringBuilder sb = new StringBuilder();

                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(parameter.Name) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(DisplayParameter(parameter)) + @"</td>");
                    if (parameter.IsOutput == true)
                        sb.Append(@"<td>Output</td>");
                    else
                        sb.Append(@"<td>Input</td>");
                    if (parameter.Description == "")
                        sb.Append(@"<td>&nbsp;</td></tr>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(parameter.Description) + @"</td></tr>");
                    
                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Extended Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_extendedProperties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('extendedProperties');"">Extended Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""extendedProperties"">");
                AddBodyLine(@"<table>");

                AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""valueColumn"" /></colgroup>");

                evenRow = true;
                foreach (SqlServer.ExtendedProperty extProperty in _storedProcedure.ExtendedProperties)
                {
                    string row = evenRow == true ? @"<tr class=""evenRow"">" : @"<tr>";
                    evenRow = !evenRow;
                    row += @"<td>" + ScrubHtml(extProperty.Name) + @"</td><td>" + ScrubHtml(extProperty.Value) + @"</td></tr>";
                    AddBodyLine(row);
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region DDL
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_ddl"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('ddl');"">DDL</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""ddl"">");
                AddBodyLine(@"<table class=""sqlCode"">");

                evenRow = true;
                var codeLines = ColorizeDDL(_storedProcedure.GetDdlTokens());
                
                foreach (string codeLine in codeLines)
                {
                    string code = "";
                    if (codeLine == "")
                        code = @"&nbsp;";
                    else
                        code = codeLine;

                    if (evenRow == true)
                        AddBodyLine(@"<tr class=""evenRow""><td>" + code + @"</td></tr>");
                    else
                        AddBodyLine(@"<tr class=""oddRow""><td>" + code + @"</td></tr>");
                    evenRow = !evenRow;
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.StoredProcedure _storedProcedure;
        #endregion
    }
}
